package com.atguigu.product;


import com.alibaba.cloud.nacos.NacosConfigManager;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;


@Slf4j
@EnableDiscoveryClient //如果使用nacos负责注册功能，需要添加此注解
@SpringBootApplication
@EnableFeignClients(basePackages = "com.atguigu")  //开启openfeign远程调用
public class ProductMainApplication {

    public static void main(String[] args) {
        SpringApplication.run(ProductMainApplication.class, args);
        log.info("商品服务启动成功********");
    }

    /**
     * 监听nacos配置文件变化
     * @param nacosConfigManager
     * @return
     */
    @Bean
    ApplicationRunner applicationRunner(NacosConfigManager nacosConfigManager) {
        return args -> {
            ConfigService configService = nacosConfigManager.getConfigService();
            //添加监听
            configService.addListener("service-product.yml", "DEFAULT_GROUP", new Listener() {
                @Override
                public void receiveConfigInfo(String configInfo) {
                    log.info("nacos配置文件发生变化: {}", configInfo);
                }

                @Override
                public Executor getExecutor() {
                    return Executors.newFixedThreadPool(5);
                }
            });
        };
    }

}
